Skip to main content

V3Pool

The UniswapV3Pool contract is a core component of the Sushi V3 AMM, implementing the decentralized exchange functionality for trading and providing liquidity for token pairs.

Read-Only Functions

snapshotCumulativesInside

function snapshotCumulativesInside(
int24 tickLower,
int24 tickUpper
) external view override returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
);

Returns the cumulative values within the specified tick range. This function is useful for off-chain consumers to get aggregated values for a specific range of ticks without needing to query each individual tick.

Parameters

NameTypeDescription
tickLowerint24lower tick of the range
tickUpperint24upper tick of the range

Returns

NameTypeDescription
tickCumulativeInsideint56difference in cumulative tick values between the tick range
secondsPerLiquidityInsideX128uint160difference in seconds per liquidity values within the tick range
secondsInsideuint32number of seconds that have passed within the tick range

Modifiers

modifier noDelegateCall();

observe

function observe(
uint32[] calldata secondsAgos
) external view override returns (
int56[] memory tickCumulatives,
uint160[] memory secondsPerLiquidityCumulativeX128s
);

Returns the tick and liquidity values for each secondsAgo specified in the input array. This function is useful for off-chain consumers to get historical data points for a pool's tick and liquidity values.

Parameters

NameTypeDescription
secondsAgosuint32[]array of seconds in the past to get values for

Returns

NameTypeDescription
tickCumulativesint56[]array of tick cumulative values for each secondsAgo
secondsPerLiquidityCumulativeX128suint160[]array of seconds per liquidity values for each secondsAgo

Modifiers

modifier noDelegateCall();

State-Changing Functions

increaseObservationCardinalityNext

function increaseObservationCardinalityNext(uint16 observationCardinalityNext)
external
override
lock
noDelegateCall

Increases the maximum number of stored observations for the pool. This allows for more historical data to be retrieved, which can be useful for off-chain consumers.

Parameters

NameTypeDescription
observationCardinalityNextuint16desired new maximum number of stored observations

Events

event IncreaseObservationCardinalityNext(uint16 oldObservationCardinalityNext, uint16 newObservationCardinalityNext);

Modifiers

modifier lock();
modifier noDelegateCall();

initialize

function initialize(uint160 sqrtPriceX96) external override

Initializes the pool with a given square root price. This function can only be called once when the pool is first created. After initialization, the pool starts tracking price and liquidity data.

Parameters

NameTypeDescription
sqrtPriceX96uint160the initial square root price for the pool (encoded as a Q64.96 fixed-point number)

Events

event Initialize(uint160 sqrtPriceX96, int24 tick);

mint

function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external override lock returns (uint256 amount0, uint256 amount1);

Mints new liquidity tokens in a specified range by depositing the underlying assets. The caller will provide the assets (amount0 and amount1) and receives liquidity tokens in return.

Parameters

NameTypeDescription
recipientaddressthe address that will receive the minted liquidity tokens
tickLowerint24the lower tick of the desired price range
tickUpperint24the upper tick of the desired price range
amountuint128the amount of liquidity to be minted
databytes calldatacallback data to be passed to uniswapV3MintCallback

Returns

NameTypeDescription
amount0uint256amount of token0 deposited
amount1uint256amount of token1 deposited

Events

event Mint(
address indexed sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

Modifiers

modifier lock();

collect

function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external override lock returns (uint128 amount0, uint128 amount1);

Collects the fees owed to a position. The position's tokensOwed{0,1} fields are reduced by the collected amounts, and the tokens are transferred to the specified recipient.

Parameters

NameTypeDescription
recipientaddressthe address that will receive the collected tokens
tickLowerint24the lower tick of the position's price range
tickUpperint24the upper tick of the position's price range
amount0Requesteduint128the amount of token0 requested to be collected
amount1Requesteduint128the amount of token1 requested to be collected

Returns

NameTypeDescription
amount0uint128amount of token0 collected
amount1uint128amount of token1 collected

Events

event Collect(
address indexed owner,
address indexed recipient,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount0,
uint128 amount1
);

Modifiers

modifier lock();

burn

function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external override lock returns (uint256 amount0, uint256 amount1);

Burns a specified amount of liquidity from a position. The position's liquidity is decreased, and the tokensOwed{0,1} fields are increased by the amounts that would be collected.

Parameters

NameTypeDescription
tickLowerint24the lower tick of the position's price range
tickUpperint24the upper tick of the position's price range
amountuint128the amount of liquidity to be burned

Returns

NameTypeDescription
amount0uint256amount of token0 owed
amount1uint256amount of token1 owed

Events

event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

Modifiers

modifier lock();

swap

function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external override noDelegateCall returns (int256 amount0, int256 amount1);

Swaps an exact input or output amount between token0 and token1, subject to a price limit, and updates the pool's state.

Parameters

NameTypeDescription
recipientaddressthe address that receives the output tokens
zeroForOnebooltrue if swapping token0 for token1, false if swapping token1 for token0
amountSpecifiedint256exact amount to swap in or out, depending on whether it is positive or negative
sqrtPriceLimitX96uint160the price limit of the swap, represented as a square root price
databytesany additional data to be passed through to the callback

Returns

NameTypeDescription
amount0int256amount of token0 swapped in or out
amount1int256amount of token1 swapped in or out

Events

event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);

Modifiers

modifier noDelegateCall();

flash

function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external override lock noDelegateCall;

This function is designed to execute a flash swap, where the user borrows amount0 of token0 and/or amount1 of token1 from the pool and repays them with a fee within the same transaction. The recipient is the address that receives the borrowed tokens. The data parameter allows passing any additional data through to the callback function.

Parameters

NameTypeDescription
recipientaddressthe address that receives the borrowed tokens
amount0uint256the amount of token0 to borrow
amount1uint256the amount of token1 to borrow
databytesany additional data to be passed through to the callback

Events

event Flash(
address indexed sender,
address indexed recipient,
uint256 amount0,
uint256 amount1,
uint256 paid0,
uint256 paid1
);

Modifiers

modifier lock();
modifier noDelegateCall();

setFeeProtocol

function setFeeProtocol(uint8 feeProtocol0, uint8 feeProtocol1) external override lock onlyFactoryOwner;

Sets the protocol fee for token0 and token1 in the pool.

Parameters

NameTypeDescription
feeProtocol0uint8the new protocol fee for token0
feeProtocol1uint8the new protocol fee for token1

Events

event SetFeeProtocol(uint8 indexed oldFeeProtocol0, uint8 indexed oldFeeProtocol1, uint8 indexed newFeeProtocol0, uint8 indexed newFeeProtocol1);

Modifiers

modifier lock();
modifier onlyFactoryOwner();

collectProtocol

function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external override lock onlyFactoryOwner returns (uint128 amount0, uint128 amount1);

This function allows the factory owner to collect protocol fees that have been accumulated in the pool for both tokens.

Parameters

NameTypeDescription
recipientaddressthe address receiving the collected protocol fees
amount0Requesteduint128the requested amount of token0 to collect
amount1Requesteduint128the requested amount of token1 to collect

Returns

NameTypeDescription
amount0uint128the actual collected amount of token0
amount1uint128the actual collected amount of token1

Modifiers

modifier lock();
modifier onlyFactoryOwner();